﻿using System.Text;

namespace LBSoft.Common.DotNetExt
{
    /// <summary>
    /// System.Text.StringBuilder的扩展方法
    /// </summary>
    public static class ExtStringBuilder
    {
        /// <summary>
        /// 从右边开始删除指定的字符串
        /// </summary>
        /// <param name="source">要删除的源StringBuilder对象</param>
        /// <param name="removeValue">要删除的自定字符串</param>
        /// <param name="leftCount">要删除的指定的字符串数量(从右边起开始算)</param>
        /// <returns></returns>
        public static StringBuilder RightRemove(this StringBuilder source, string removeValue, int rightCount)
        {
            if (source.Length < rightCount)
            {
                return source.Replace(removeValue, string.Empty);
            }
            return source.Replace(removeValue, string.Empty, source.Length - rightCount, rightCount);
        }


        /// <summary>
        /// 从左边开始删除指定的字符串
        /// </summary>
        /// <param name="source">要删除的源StringBuilder对象</param>
        /// <param name="removeValue">要删除的自定字符串</param>
        /// <param name="leftCount">要删除的指定的字符串数量(从左边起开始算)</param>
        /// <returns></returns>
        public static StringBuilder LeftRemove(this StringBuilder source, string removeValue, int leftCount)
        {
            if (0 > leftCount)
            {
                return source;
            }
            return source.Replace(removeValue, string.Empty, 0, leftCount);
        }
    }
}
